package com.czz.manage.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Audit;
import com.czz.manage.query.AuditQuery;
import com.czz.manage.service.AuditService;
import com.czz.manage.vo.AuditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: AuditAdminController
 * @Author Czz
 * @Date 2020/1/8
 * @Time 10:06
 **/
@RestController
@RequestMapping(value = "/admin/audits")
public class AuditAdminController {

    @Autowired
    AuditService auditService;

    /**
    * @Description 分页查询讲师审核列表
    * @Param [page, limit, auditQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/9
    * @Time 15:51
    **/
    @GetMapping(value = "/{page}/{limit}")
    public R getAuditVoPageList(@PathVariable Long page, @PathVariable Long limit, AuditQuery auditQuery){
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<AuditVo> pageParam = new Page<>(page, limit);
        pageParam.setRecords(auditService.getAuditVoPageList(pageParam,auditQuery));
        List<AuditVo> records = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
    * @Description 修改讲师审核信息
    * @Param [id, auditVo]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/10
    * @Time 16:03
    **/
    @PutMapping(value = "/{id}")
    public R updateAuditStatusById(@PathVariable String id,AuditVo auditVo){
        if (StringUtils.isEmpty(id) || ObjectUtils.isEmpty(auditVo)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        auditVo.setAuditId(id);
        //修改讲师审核信息
        if (StringUtils.isEmpty(auditVo.getStatus())){
            auditService.updateAuditById(auditVo);
            return R.ok();
        }

        //修改讲师审核状态
        auditService.updateAuditStatusById(auditVo);
        return R.ok();
    }

    /**
    * @Description 根据id获取讲师审核信息
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/10
    * @Time 16:03
    **/
    @GetMapping(value = "/{id}")
    public R getAuditById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Audit audit = auditService.getById(id);
        return R.ok().data("item",audit);
    }
}
